﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MhAdmin.Application.Services.Students.Dto;
using MhAdmin.SqlSugar.Extension;
using MhAdmin.System.Entity;

namespace MhAdmin.Application.Services.Students
{
    public class StudentServices : DbRepository<Student>, IStudentServices
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task Add(StudentAddInput input)
        {
            var sudent = input.Adapt<Student>();
            await InsertAsync(sudent);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task Delete(List<DeleteStudent> student)
        {
            var ids = student.Select(x => x.Id).ToList();
            await Context.FalseDeleteAsync<Student>(ids);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="edit"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task Edit(EditStudent edit)
        {
            var sudent = edit.Adapt<Student>();
            await UpdateAsync(sudent);
        }
        /// <summary>
        /// 根据单条显示
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Student> GetStudentById(DeleteStudent student)
        {
            var sudent = student.Adapt<Student>();
            return await GetByIdAsync(sudent.Id);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public async Task<SqlSugarPagedList<Student>> Page(PageStudentInput input)
        {
            var pageList = Context.Queryable<Student>().WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name)).OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}").OrderBy(x=>x.Name);
            //分页
            SqlSugarPagedList<Student> pageinfo = await pageList.ToPagedListAsync(input.Current, input.Size);
            return pageinfo;
        }
    }
}
